//
//  PQ_baseViewController.m
//  PaiQuan
//
//  Created by Mac on 16/7/6.
//  Copyright © 2016年 huigu. All rights reserved.
//

#import "PQ_baseViewController.h"
#import "Reachability.h"
#import "JH_NoStateView.h"
@interface PQ_baseViewController ()<JHTitleViewDelegate>

{
    //loadingUI的起始位置
    CGFloat marginTop;
    CGFloat marginBottom;
}
/**
 *  网络变化UI
 */
@property (nonatomic,strong) UIButton *netWorkChageBtn;
/**
 *  网络加载UI
 */
@property (nonatomic,strong) NetworkLoadView *netWorkLoadIngView;

@end

@implementation PQ_baseViewController
#pragma mark - 控制器相关
-(instancetype)init
{
    if (self = [super init])
    {
        [self setupTitleView];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setupTitleView];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
    self.view.backgroundColor = PQ_COLOR_DarkGray;
    self.navigationController.navigationBarHidden = YES;
    [self.view addSubview:self.titleView];
    self.titleView.title = self.title;
    marginTop = NaviBarHeight;
    marginBottom = 0;
    if (self.navigationController.childViewControllers.count == 1)
    {
        self.titleView.isHidebtnBack = YES;
    }
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    // Do any additional setup after loading the view, typically from a nib.
    self.automaticallyAdjustsScrollViewInsets = NO;


    

    
  
}


-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent ;
}
- (void)setupTitleView
{
    self.titleView = [[JHTitleView alloc] initWithFrame:CGRectMake(0,0, ScreenW, NaviBarHeight)];
    self.titleView.delegate = self;
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
        // titleView 最上面
    
    // pop到外面的时候 把tabbar还给人家
    for (UITabBar *tabbar in self.view.subviews)
    {
        if ([tabbar isKindOfClass:[UITabBar class]])
        {
            [self.tabBarController.view addSubview:tabbar];
            marginBottom = 49;
        }
    }
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.view bringSubviewToFront:self.titleView];

}

- (void)removeSuperTitleView
{
    for (UIView *temp in self.view.subviews)
    {
        // 因为sb创建的 这个title就不要了
        if ([temp isKindOfClass:[JHTitleView class]])
        {
            marginTop = 0;
            [temp removeFromSuperview];
        }
    }
}
-(void)jhTitleView:(JHTitleView *)titleView clickBackButton:(UIButton *)backbtn
{
    if (self.navigationController.topViewController == self)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - 网络加载UI
- (void)showLoadingUI
{
    [self showLoadingUIWithNetWorkLoadType:NetWorkLoadTypeDefault andImageAnimation:nil andPromptText:nil];
}
- (void)showLoadingUIWithNetWorkLoadType:(NetWorkLoadType)netWorkLoadType
                       andImageAnimation:(NSArray *)imageAnimationArray
                           andPromptText:(NSString *)promptText
{
    [self.netWorkLoadIngView netLoadIngType:netWorkLoadType andImageAnimation:imageAnimationArray andPromptText:promptText];
    switch (netWorkLoadType) {
        case NetWorkLoadTypeDefault: {
            [self.netWorkLoadIngView.indicatorView startAnimating];
            break;
        }
        case NetWorkLoadTypeImageAnimation: {
        }
        case NetWorkLoadTypeImageAnimationAndPromptText: {
            [self.netWorkLoadIngView.imageView startAnimating];
        }
            break;
        default: {
            break;
        }
    }
    
}
/**留个接口子类自定义*/
- (void)showCustomLoadingUI
{
}
- (void)hideLoadingUI
{
    if (_netWorkLoadIngView && !_netWorkLoadIngView.hidden) {
        [UIView animateWithDuration:0.3 animations:^{
            self.netWorkLoadIngView.alpha = 0;
        } completion:^(BOOL finished) {
            if (self.netWorkLoadIngView.netWorkLoadType == NetWorkLoadTypeDefault) {
                [self.netWorkLoadIngView.indicatorView stopAnimating];
            }else{
                [self.netWorkLoadIngView.imageView stopAnimating];
            }
            self.netWorkLoadIngView.hidden = YES;
        }];
    }
}

#pragma mark - 消息提示
//默认只提示文字
- (void)showStateText:(NSString *)text {
    [self showPromptTextUIWithPromptText:text title:nil andDuration:3];
}
- (void)showPromptTextUIWithPromptText:(NSString *)promptText
                                 title:(NSString*)title
                           andDuration:(NSTimeInterval)interval
{
    if (interval == 0) {
        interval = 3;
    }
    if (!promptText.length) {
        promptText = @"加载中";
    }
    if (!stateHud) {
        stateHud = [[MBProgressHUD alloc] initWithView:self.view];
        stateHud.delegate = self;
        [self.view addSubview:stateHud];
    }
    stateHud.mode = MBProgressHUDModeText;
    stateHud.detailsLabelText = promptText;
    stateHud.detailsLabelFont = [UIFont systemFontOfSize:13.0f];
    [stateHud show:YES];
    [stateHud hide:YES afterDelay:interval];
    
}
- (void)showAlertViewWithMessage:(NSString *)message
                        andTitle:(NSString *)title
                     andDelegate:(id<UIAlertViewDelegate>)delegate
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title
                                                       message:message
                                                      delegate:delegate
                                             cancelButtonTitle:@"取消"
                                             otherButtonTitles:@"确定", nil];
    [alertView show];
}

#pragma mark -- 网络加载UI
/**网络加载UI*/
- (NetworkLoadView *)netWorkLoadIngView
{
    if (!_netWorkLoadIngView) {
        _netWorkLoadIngView = ({
            NetworkLoadView *loadIngView  =[[NetworkLoadView alloc]initWithFrame:CGRectMake(0, NaviBarHeight, ScreenW, self.view.frame.size.height - marginBottom - marginTop)];
            [self.view addSubview:loadIngView];
            loadIngView;
        });
    }
    return _netWorkLoadIngView;
}

#pragma mark -- 空状态提示

- (JH_NoStateView *)noStateView {
    if (!_noStateView) {
        _noStateView = [[JH_NoStateView alloc] initWithFrame:CGRectMake((ScreenW - RATIO(260)) / 2, (ScreenH - RATIO(260) - RATIO(40)) / 2, RATIO(260), RATIO(260) + RATIO(40))];
        _noStateView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:_noStateView];
    }
    return _noStateView;
}
- (void)showNoStateWithImage:(UIImage *)image andTitle:(NSString *)title {
    [self.noStateView configureNoStateViewWithImage:image Title:title];
}

#pragma mark - 网络不可用点击UI的回调  (去设置界面)
- (void)goNetNotUse:(UIButton *)sender
{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
